layui.define(['form','table','zTree'], function (exports) {
    "use strict";
    var LayoutTree = function () {
            this.v = '0.1';
        }, _MOD = 'layoutTree',
        _this = this,
        form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        zTree = layui.zTree;

        LayoutTree.prototype.render = function (options) {
                var opts = options,
            //关于zTree http://www.treejs.cn/v3/api.php的Api

                treeelem=opts.treeelem,//zTree显示div的id
                 elem = opts.elem,//table的id
                url = opts.url,//zTree类型数据获取的URL
                rootId=opts.rootId,
                check=opts.check,//是否进行勾选
                value=opts.value,//勾选的值
                search=opts.search;//需要查询的值
                   var defaults = {
                       async: {
                           enable: true,
                           url: url,
                           // otherParam: ["isAll",${isAll!},"type",${type!},'extId','${extId!}'],
                           type: "GET"
                       },
                       data: {
                           simpleData: {
                               enable: true,
                               idKey: "id",
                               pIdKey: "pId",
                               rootPId: rootId
                           }
                       },
                       check: {
                           enable: check === 'true' ? true : false,
                           nocheckInherit: true,      // 设置子节点是否自动继承
                       },
                       callback: {
                           onDblClick: function (event, treeId, treeNode) {
                               // var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                               // $(obj.elem, parent.document).val(treeNode.id);
                               // $(obj.showElem, parent.document).children('input').val(treeNode.name);
                               // parent.layer.close(index);
                           },
                           onClick: function (event, treeId, treeNode) {
                               var currZtree = zTree.getZTreeObj(treeId);
                               var expandFlag = treeNode.open ? false : true;
                               currZtree.expandNode(treeNode, expandFlag, false, true),
                               defaults.TreeOnClick(treeNode.id); // 用于捕获节点被点击的事件回调函数
                           },
                           onAsyncSuccess: function (event, treeId, treeNode) {
                               let currZtree = zTree.getZTreeObj(treeId);
                               let nodeList = currZtree.getNodes();
                               currZtree.expandNode(nodeList[0], true);

                               if (check === 'true') {
                                   let pnodes = [];
                                   let ids = value.split(',');
                                   ids.forEach(function (item, index) {
                                       let node = currZtree.getNodeByParam("id", item, null) || '';
                                       node.checked = true;
                                       currZtree.updateNode(node);
                                       let pnode = currZtree.getNodeByParam("id", node.pId, null);
                                       pnodes.push(pnode);
                                   });
                                   if (pnodes[0] != null) {
                                       pnodes.forEach(function (node, index) {
                                           node.checked = true;
                                           currZtree.updateNode(node);
                                           currZtree.expandNode(node, true);
                                       });
                                   }
                               } else {
                                   let node = currZtree.getNodeByParam("id", value, null) || '';
                                   currZtree.selectNode(node);
                                   let pnode = currZtree.getNodeByParam("id", node.pId, null);
                                   currZtree.expandNode(pnode, true);
                               }
                           }
                       },

                       TreeOnClick:function (id) {
                       var queryData = {};
                       queryData[''+search+''] =id;
                       table.reload(""+elem+"", {
                           where: queryData, page: {curr: 1}
                       });
                   }

                   };
               zTree.init($(""+treeelem+""), defaults);

        };


    var layoutTree = new LayoutTree();
    exports(_MOD, layoutTree);
});

